package com.mujin.keji.collectionsystem.module.outboundtask.viewmodel;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.ViewModel;

import com.alibaba.fastjson.JSONObject;
import com.mujin.keji.collectionsystem.base.BaseDto;
import com.mujin.keji.collectionsystem.module.outboundtask.dto.ApplyCaseDto;
import com.mujin.keji.collectionsystem.module.outboundtask.dto.QueryContactDto;
import com.mujin.keji.collectionsystem.module.outboundtask.dto.QueryUserDto;
import com.mujin.keji.collectionsystem.module.outboundtask.dto.StatusCountDto;
import com.mujin.keji.collectionsystem.module.outboundtask.dto.VisitRecordDetailDto;
import com.mujin.keji.collectionsystem.module.outboundtask.dto.VisitRecordDto;
import com.mujin.keji.collectionsystem.module.outboundtask.repository.ApplyCaseRepository;
import com.mujin.keji.collectionsystem.module.outboundtask.repository.BeginVisitRepository;
import com.mujin.keji.collectionsystem.module.outboundtask.repository.IApplyCaseRepository;
import com.mujin.keji.collectionsystem.module.outboundtask.repository.IBeginVisitRepository;
import com.mujin.keji.collectionsystem.module.outboundtask.repository.IQueryContactRepository;
import com.mujin.keji.collectionsystem.module.outboundtask.repository.IQueryUserRepository;
import com.mujin.keji.collectionsystem.module.outboundtask.repository.IStatusCountRepository;
import com.mujin.keji.collectionsystem.module.outboundtask.repository.IVisitRecordDetailRepository;
import com.mujin.keji.collectionsystem.module.outboundtask.repository.IVisitRecordRepository;
import com.mujin.keji.collectionsystem.module.outboundtask.repository.QueryContactRepository;
import com.mujin.keji.collectionsystem.module.outboundtask.repository.QueryUserRepository;
import com.mujin.keji.collectionsystem.module.outboundtask.repository.StatusCountRepository;
import com.mujin.keji.collectionsystem.module.outboundtask.repository.VisitRecordDetailRepository;
import com.mujin.keji.collectionsystem.module.outboundtask.repository.VisitRecordRepository;
import com.mujin.keji.collectionsystem.module.outboundtask.vo.ApplyCaseVo;
import com.mujin.keji.collectionsystem.module.outboundtask.vo.BeginVisitVo;
import com.mujin.keji.collectionsystem.module.outboundtask.vo.QueryContactVo;
import com.mujin.keji.collectionsystem.module.outboundtask.vo.QueryUserVo;
import com.mujin.keji.collectionsystem.module.outboundtask.vo.SaveVisitRecordVo;
import com.mujin.keji.collectionsystem.module.outboundtask.vo.StatusCountVo;
import com.mujin.keji.collectionsystem.module.outboundtask.vo.VisitRecordDetailVo;
import com.mujin.keji.collectionsystem.module.updata.dto.ShowVisitUserDto;
import com.mujin.keji.collectionsystem.module.updata.dto.VisitUserDto;
import com.mujin.keji.collectionsystem.utils.EmptyUtils;
import com.mujin.keji.collectionsystem.utils.FileUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 外访列表viewmodel
 *
 * @author weishixiong
 * @Time 2018-05-10
 */
public class BoundTaskListViewModel extends ViewModel {

    /**
     * 外访任务
     */
    private LiveData<BaseDto<ApplyCaseDto>> liveData;
    /**
     * 外放任务统计
     */
    private LiveData<BaseDto<List<StatusCountDto>>> statusCountLiveData;

    /**
     * 外放详情
     */
    private LiveData<BaseDto<VisitRecordDetailDto>> visitRecordDetailLiveData;
    /**
     * 保存外访
     */
    private LiveData<BaseDto<VisitRecordDto>> VisitRecordLiveData;
    /**
     * 协助专员
     */
    private LiveData<BaseDto<QueryUserDto>> userDtoLiveData;
    /**
     * 判断是否开始外访的时间a
     */
    private LiveData<BaseDto<String>> beginVisitLiveData;

    /**
     * 案件联系人
     */
    private LiveData<BaseDto<QueryContactDto>> contactDtoLiveData;
    /**
     * 查询案件联系人
     *
     * @param caseId
     * @return
     */
    public LiveData<BaseDto<QueryContactDto>> getContactDtoLiveData(String caseId) {
        IQueryContactRepository queryContactRepository = new QueryContactRepository();
        QueryContactVo vo = new QueryContactVo();
        vo.setCaseId(caseId);
        this.contactDtoLiveData = queryContactRepository.queryContact(vo);
        return contactDtoLiveData;
    }

    public LiveData<BaseDto<QueryUserDto>> getUserDtoLiveData(String userName) {
        IQueryUserRepository queryUserRepository = new QueryUserRepository();
        QueryUserVo vo = new QueryUserVo(userName);
        this.userDtoLiveData = queryUserRepository.queryUser(vo);
        return userDtoLiveData;
    }

    /**
     * 保存外访
     *
     * @param visitRecordVo
     * @return
     */
    public LiveData<BaseDto<VisitRecordDto>> getVisitRecordLiveData(SaveVisitRecordVo visitRecordVo) {
        IVisitRecordRepository visitRecordRepository = new VisitRecordRepository();
        this.VisitRecordLiveData = visitRecordRepository.saveVisitRecord(visitRecordVo);
        return VisitRecordLiveData;
    }

    /**
     * 外访详情
     */
    public LiveData<BaseDto<VisitRecordDetailDto>> getVisitRecordDetailLiveData(Long applyId) {
        IVisitRecordDetailRepository visitRecordDetailRepository = new VisitRecordDetailRepository();
        VisitRecordDetailVo vo = new VisitRecordDetailVo(applyId);
        this.visitRecordDetailLiveData = visitRecordDetailRepository.visitRecord(vo);
        return visitRecordDetailLiveData;
    }

    /**
     * 查询外访任务统计
     *
     * @param vo
     * @return
     */
    public LiveData<BaseDto<List<StatusCountDto>>> getStatusCountLiveData(StatusCountVo vo) {
        IStatusCountRepository statusCountRepository = new StatusCountRepository();
        this.statusCountLiveData = statusCountRepository.count(vo);
        return statusCountLiveData;
    }

    /**
     * 查询外访任务列表
     *
     * @param vo
     * @return
     */
    public LiveData<BaseDto<ApplyCaseDto>> getLiveData(ApplyCaseVo vo) {
        IApplyCaseRepository applyCaseRepository = new ApplyCaseRepository();
        this.liveData = applyCaseRepository.queryApplyCase(vo);
        return liveData;
    }


    /**
     * 判断是否开始外访
     *
     * @param vo
     * @return
     */
    public LiveData<BaseDto<String>> beginVisit(BeginVisitVo vo) {
        IBeginVisitRepository beginVisitRepository = new BeginVisitRepository();
        this.beginVisitLiveData = beginVisitRepository.beginVisit(vo);
        return beginVisitLiveData;
    }
    ////

    private List<ShowVisitUserDto> showVisitUserDtoList;
    private List<String> applyIdList;


    public ShowVisitUserDto getShowVisitUserDtoList(Long id) {
        try {
            showVisitUserDtoList = new ArrayList<>();
            applyIdList = new ArrayList<>();
            for (int i = 0; i < visitUserDtos.size(); i++) {
                String fileName = visitUserDtos.get(i).getFileName();
                for (int k = 0; k < photoAndRecordFiles.size(); k++) {
                    if (photoAndRecordFiles.get(k).getName().contains(fileName)) {
                        visitUserDtos.get(i).setFile(photoAndRecordFiles.get(k));
                    }
                }
                //判断是否包含key
                if (applyIdList.contains(visitUserDtos.get(i).getVisitApplyId())) {
                    int index = applyIdList.indexOf(visitUserDtos.get(i).getVisitApplyId());
                    showVisitUserDtoList.get(index).getVisitUserDtoList().add(visitUserDtos.get(i));
                } else {
                    ShowVisitUserDto showVisitUserDto = new ShowVisitUserDto();
                    showVisitUserDto.setVisitApplyId(visitUserDtos.get(i).getVisitApplyId());
                    List<VisitUserDto> v = new ArrayList<>();
                    v.add(visitUserDtos.get(i));
                    showVisitUserDto.setVisitUserDtoList(v);
                    showVisitUserDtoList.add(showVisitUserDto);
                    applyIdList.add(visitUserDtos.get(i).getVisitApplyId());
                }
            }
            return showVisitUserDtoList.get(applyIdList.indexOf(String.valueOf(id)));

        } catch (Exception E) {
            return null;
        }


    }

    private List<File> photoAndRecordFiles = new ArrayList<>();
    private List<VisitUserDto> visitUserDtos = new ArrayList<>();
    private List<File> fileList = new ArrayList<>();

    /**
     * 获取文件成功回调
     * 刷新列表
     * id
     *
     * @param files
     */
    public void initFiles(List<File> files) {
        fileList.clear();
        visitUserDtos.clear();
        photoAndRecordFiles.clear();
        if (EmptyUtils.isNotEmpty(files)) {
            fileList.addAll(files);
            for (int i = 0; i < files.size(); i++) {
                if (files.get(i).getName().endsWith(".txt")) {
                    try {
                        String visitStr = FileUtil.readJsonFromFile(files.get(i));
                        visitUserDtos.add(JSONObject.parseObject(visitStr, VisitUserDto.class));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    photoAndRecordFiles.add(files.get(i));
                }

            }

        }
    }
}
